/**
 * 
 */
package net.xfok.db;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author user
 *
 */
public class Reflect {
	
	private static final String GET="get";
	private static final String SET="set";
	
	public static Map<String,String> genFields(Object obj) {
		Map<String,String> map = new LinkedHashMap<String,String>();
		Field[] fields = genFileds(obj);
		for (Field field : fields) {
			map.put(field.getName(), field.getType().getSimpleName());
		}
		return map;
	}
	
	public static Field[] genFileds(Object obj) {
		return obj.getClass().getDeclaredFields();
	}

	public static Field genField(Object obj, String fieldName){
		try{
			return obj.getClass().getField(fieldName);
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}

	public static Method genGetMethod(Object obj, Field field) {
		try{
			String fieldName = field.getName();		
			return genGetMethod(obj, fieldName);
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	public static Method genGetMethod(Object obj, String fieldName) {
		try{
			String stringLetter = fieldName.substring(0, 1).toUpperCase();
			String getName = GET + stringLetter + fieldName.substring(1);
			return obj.getClass().getMethod(getName, new Class[] {});
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}

	public static Method genSetMethod(Object obj, Field field){
		try{
			String fieldName = field.getName();
			String stringLetter = fieldName.substring(0, 1).toUpperCase();
			String setName = SET + stringLetter + fieldName.substring(1);
			return obj.getClass().getMethod(setName, new Class[] { field
					.getType() });
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	public static Method genSetMethod(Object obj, String fieldName){
		try{
			String stringLetter = fieldName.substring(0, 1).toUpperCase();
			String setName = SET + stringLetter + fieldName.substring(1);
			return obj.getClass().getMethod(setName, new Class[] { genField(obj, fieldName)
					.getType() });
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	public static  Object invokeGet(Object obj, String fieldName){
		Method getMethod = genGetMethod(obj, genField(obj, fieldName));
		return invokeGet(obj, getMethod);
	}
	
	public static  Object invokeGet(Object obj, Field field){
		Method getMethod = genGetMethod(obj, field);
		return invokeGet(obj, getMethod);
	}
	
	public static Object invokeGet(Object obj, Method getMethod){
		try{
			return getMethod.invoke(obj, new Object[] {});
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}

	public static void invokeSet(Object obj, String fieldName, Object value) {
		Method setMethod=genSetMethod(obj, genField(obj, fieldName));
		invokeSet(obj, setMethod, value);
	}
	
	public static void invokeSet(Object obj, Field field, Object value) {
		Method setMethod=genSetMethod(obj, field);
		invokeSet(obj, setMethod, value);
	}

	public static void invokeSet(Object obj, Method setMethod, Object value){
		try{
			setMethod.invoke(obj, new Object[] { value });
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
}
